package com.konsung.ksdc.manager.advice;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.ReplyNetBean;
import com.konsung.ksdc.bean.netbean.SuggestionNetBean;
import com.konsung.ksdc.common.utils.DateUtils;
import com.konsung.ksdc.config.MyApplication;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/3/18 0018.
 */
public class AdviceDetailActivity extends BaseActivity {

    @Bind(R.id.tab_tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.tv_state)
    TextView tvState;
    @Bind(R.id.lv_reply_list)
    ListView lvReplyList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advice_detail);

        ButterKnife.bind(this);
        SuggestionNetBean bean = getIntent().getParcelableExtra(AdviceActivity.ADVICE_ITEM_CLICKED);
        //意见与建议排序
        List<ReplyNetBean> replies = bean.getReplies();
        //升序排列
        Collections.sort(replies, new Comparator<ReplyNetBean>() {
            @Override
            public int compare(ReplyNetBean lhs, ReplyNetBean rhs) {

                return DateUtils.compareDateTime(lhs, rhs);
            }
        });
        //回复按照降序排列
        Collections.reverse(replies);
        initView();
        initData(bean);
    }

    @Override
    protected String activityDesc() {
        return getString(R.string.page_advice_detail);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        tvTitle.setText(R.string.title_advice_detail);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 初始化数据
     * @param bean 数据
     */
    private void initData(SuggestionNetBean bean) {

        String createDate = bean.getCreateDate();
        String str = createDate.replace("&", "  ");
        String date = str.replace("_", ":");
        tvDate.setText(date);

        tvContent.setText(bean.getSuggestionContent());
        tvState.setText(parseState(bean.getSuggestionState()));

        lvReplyList.setAdapter(new ReplyDetailAdapter(bean.getReplies()));
    }

    /**
     * 解析数据
     * @param stateCode 状态code
     * @return 数据
     */
    private String parseState(String stateCode) {
        int color = MyApplication.getContext().getResources().getColor(R.color.text_normal);

        if (SuggestionNetBean.STATE_NO_REPLIED.equals(stateCode)) {
            tvState.setTextColor(color);
            return MyApplication.getContext().getString(R.string.state_no_reply);
        }

        color = MyApplication.getContext().getResources().getColor(R.color.title_text_bg_pressed);
        tvState.setTextColor(color);
        return MyApplication.getContext().getString(R.string.state_replied);
    }
}
